﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TaiwuZD
{
    /// <summary>
    /// ActorGongFa.xaml 的交互逻辑
    /// </summary>
    public partial class ActorGongFa : UserControl
    {
        public static CollectionViewSource TotalGongFaSource { get; set; } = new CollectionViewSource();
        public static CollectionViewSource OwnedGongFaSource { get; set; } = new CollectionViewSource();

        public static RoutedCommand ForgetGongFaCommand { get; set; } = new RoutedCommand();
        public static RoutedCommand LearnGongFaCommand { get; set; } = new RoutedCommand();


        public ActorGongFa()
        {
            InitializeComponent();

            TotalGongFaSource.Source = GameAssets.TwGongFas.Values;
            TotalGongFaSource.Filter += TotalGongFaSource_Filter;

            OwnedGongFaSource.Source = Global.Inst.CurrentRole?.OwnedGongFas;
            OwnedGongFaSource.Filter += TotalGongFaSource_Filter; 

            Global.Inst.PropertyChanged += Inst_PropertyChanged;
        }

        private void TotalGongFaSource_Filter(object sender, FilterEventArgs e)
        {
            var tid = ((KeyValuePair<int, string>)lvGongFaType.SelectedItem).Key;
            var sid = ((KeyValuePair<int, string>)lvMenPai.SelectedItem).Key;
            var gf = (e.Item as TwGongFa);

            if (tid != gf.TypeId)
                e.Accepted = false;
            else if (sid != -1 && sid != gf.SchoolId)
                e.Accepted = false;
            else
                e.Accepted = true;
        }

        private void lvGongFaType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TotalGongFaSource?.View?.Refresh();
            OwnedGongFaSource?.View?.Refresh();
        }
        private void Inst_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Global.CurrentRole))
            {
                OwnedGongFaSource.Source = Global.Inst.CurrentRole.OwnedGongFas;
            }
        }
        

        private void ForgetGongFaCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = lvGongFaOwned?.SelectedItem != null;
        }

        private void ForgetGongFaCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var gf = lvGongFaOwned.SelectedItem as TwGongFa;
            Global.Inst.CurrentRole.OwnedGongFas.Remove(gf);
        }

        private void LearnGongFaCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Global.Inst.IsSFLoaded && lvGongFaGallay?.SelectedItem != null;
        }

        private void LearnGongFaCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var gf = lvGongFaGallay.SelectedItem as TwGongFa;

            if (gf != null)
            {
                foreach (var item in lvGongFaGallay.SelectedItems)
                {
                    gf = item as TwGongFa;
                    var ngf = gf.Clone() as TwGongFa;
                    ngf.Proficiency = 100;
                    ngf.Degree = 10;
                    ngf.Reverse = 0;
                    Global.Inst.CurrentRole.OwnedGongFas.Add(ngf);
                }
                lvGongFaGallay.SelectedItems.Clear();
            }
        }
    }
}
